<?php
/**
 * Inter组件之Session存储器：Memcache
 *
 * @author Horse Luke<horseluke@126.com>
 * @copyright Horse Luke, 2010
 * @license the Apache License, Version 2.0 (the "License"). {@link http://www.apache.org/licenses/LICENSE-2.0}
 * @version $Id: Memcache.php 153 2011-02-04 09:48:39Z horseluke@126.com $
 * @package Inter_Session
 * @subpackage Inter_Session_Storage
 */
class Inter_Session_Storage_Memcache extends Inter_Session_Storage_Abstract{
    
    /**
     * 配置选项数组
     * @var array
     */
    protected $_config = array();
    
    /**
     * Memcache实例
     * @var Memcache
     */
    protected $_MEMOBJ = null;

    /**
     * 是否已经连接？
     * @var bool
     */
    protected $_connected = false;
    
    /**
     * 构造函数
     */
    public function __construct(){
        $this->_init();
    }
    
    /**
     * 打开一个session
     * @param string $save_path
     * @param string $session_name
     * @return bool
     */
    public function open($save_path, $session_name){
        $this->_connect();
        return true;
    }
    
    /**
     * 关闭一个session
     * Memcache自带GC，故无需调用GC
     * @return bool
     * 
     */
    public function close(){
        //$this->_disconnect();
        return true;
    }
    
    /**
     * 读取一个session
     * @param string $id session id
     * @return string  session数据。按原样传出字符串
     */
    public function read($id){
        if(false == $this->_connected){
            return '';
        }
        return strval($this->_MEMOBJ->get($this->_getSessId($id)));
    }
    
    /**
     * 写入一个session
     * @param string $id session id
     * @param string $sess_data session数据。为兼容php自身行为，请保证传入的是字符串
     */
    public function write($id, $sess_data){
        if(false == $this->_connected){
            return false;
        }
        return $this->_MEMOBJ->set($this->_getSessId($id), $sess_data, 0, $this->_config['session.gc_maxlifetime']);
    }
    
    /**
     * 销毁一个session
     * @param string $id session id
     */
    public function destroy($id){
        if(false == $this->_connected){
            return false;
        }
        return $this->_MEMOBJ->delete($this->_getSessId($id), 0);
    }
    
    /**
     * session回收
     * Memcache自带GC，故无需调用GC
     * @param integer $maxlifetime session存活时间
     */
    public function gc($maxlifetime){
        return true;
    }
    
    /**
     * 初始化本类相关资源
     */
    protected function _init(){
        $this->_readConfig();
    }
    
    /**
     * 读取本类的配置文件，并且存放到$this->_config中
     */
    protected function _readConfig(){
        $this->_config = require dirname(__FILE__). '/../../../../Config/'. __CLASS__. '.php';
    }
    
    /**
     * 连接Memcache
     */
    protected function _connect(){
        if(false == $this->_connected){
            $this->_MEMOBJ = new Memcache();
            if(true == $this->_config['PCCONNECT']){
                $result = $this->_MEMOBJ->pconnect($this->_config['HOST'], $this->_config['PORT'], $this->_config['TIMEOUT']);
            }else{
                $result = $this->_MEMOBJ->connect($this->_config['HOST'], $this->_config['PORT'], $this->_config['TIMEOUT']);
            }
            
            if(false == $result){
                throw new Exception('Can not connect to Memcache Server');
            }
            $this->_connected = true;
        }
    }
    
    /*
     * 断开Memcache连接
     */
    /*
    protected function _disconnect(){
        if(true == $this->_connected){
            if(is_object($this->_MEMOBJ)){
                $this->_MEMOBJ->close();
            }
            $this->_MEMOBJ = null;
            $this->_connected = false;
        }
    }
    */
    
    /**
     * 获取session id在Memcache的真正cache id
     * @return string
     */
    protected function _getSessId($id){
        return 'sess_'. $id;
    }
    
}